package com.zrrd._01_servlet.mapper;

import com.zrrd._01_servlet.pojo.Student;

import java.sql.*;

/**
 * @author WanJl
 * @version 1.0
 * @title StudentMapper
 * @description
 * @create 2024/1/13 17:40
 */
public class StudentMapper {
    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    public boolean login(String username,String password){
        return false;
    }

    /**
     * 用户注册
     * @param student 要注册的对象，将对象里的属性变量传递到数据库中
     * @return
     */
    public boolean register(Student student) throws ClassNotFoundException, SQLException {
        boolean flag=false;
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.建立连接
        Connection conn =
                DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jms2301_db01",
                        "root", "123456");
        //3.编写sql语句
        String sql="INSERT INTO jms2301_db01.student05 (username, password, realname, age, sex) VALUES (?,?,?,?,?)";
        //4.创建Statement对象
        PreparedStatement pstat = conn.prepareStatement(sql);
        pstat.setString(1,student.getUsername());
        pstat.setString(2,student.getPassword());
        pstat.setString(3,student.getRealName());
        pstat.setInt(4,student.getAge());
        pstat.setString(5,student.getSex());
        //5.执行sql
        int result = pstat.executeUpdate();
        //6.处理结果
        if (result>0){
            flag=true;
        }
        //7.释放资源
        pstat.close();
        conn.close();

        return flag;
    }
}
